<#include "/common/taglibs.ftl" />
<#include "/personal/message/taglibs/messageView.ftl" />
<#include "/personal/message/taglibs/messageFromToDesc.ftl" />
<@menu code='personal.message'/>
<title><@spring.message code="${entityName!}" /> - <@spring.message code="${op}" /></title>
<div class="m-portlet">
    <div class="m-portlet__head">
        <div class="m-portlet__head-caption">
            <div class="m-portlet__head-title">
                <span class="m-portlet__head-icon">
                    <i class="flaticon-calendar"></i>
                </span>
                <h3 class="m-portlet__head-text m--font-primary">
                <@spring.message code="${entityName!}" /> / <@spring.message code="${op}" />
                <#if op != 'op.view'>
                    （<@spring.message code="reuqired.tips"/>）
                </#if>
                </h3>
            </div>
        </div>
    <@formHeaderButtons resourceIdentity='${resourceIdentity!}' baseUrl='${baseUrl!}'/>
    </div>
    <#if op == 'op.reply'>
    <div class="panel-body">
        <div class="panel-group accordion" id="accordion">
            <@messageView message=parent collapse="in"/>
        </div>
    </div>
    </#if>
    <!--begin::Form-->
    <form class="m-form m-form--fit m-form--label-align-right" method="post" id="mainForm">
        <div class="m-portlet__body">
            <div class="m-form__content">
                <@showFieldErrors commandName="m" />
                <#include "/common/taglibs/validationMessage.ftl">
            </div>
            <@BackURL hiddenInput="true"/>
            <input type="hidden" class="form-control" id="id" name="id" value="${(m.id)!}"/>
            <input type="hidden" class="form-control" id="parentId" name="parentId" value="${(m.parentId)!}"/>
            <input type="hidden" class="form-control" id="parentIds" name="parentIds" value="${(m.parentIds)!}"/>
            <input type="hidden" class="form-control" id="receiverId" name="receiverId" value="${(m.receiverId)!}"/>

            <#if (op == 'op.send.new') | (op == 'op.forward') | (op == 'op.send.draft')>
                <div class="form-group m-form__group row">
                    <label class="col-form-label col-lg-3 col-sm-12"><@spring.message code="message.receiver"/><span class="required"> * </span></label>
                    <div class="col-lg-4 col-md-9 col-sm-12">
                        <input type="text" id="receiverId_msg" value="${receiverName!}" class="form-control">
                    </div>
                </div>
            </#if>
            <div class="form-group m-form__group row">
                <label class="col-form-label col-lg-3 col-sm-12"><@spring.message code="notification.title"/><span class="required"> * </span></label>
                <div class="col-lg-4 col-md-9 col-sm-12">
                    <input type="text" placeholder="" class="form-control" name="title" value="${(m.title)!}" required/>
                </div>
            </div>
            <div class="form-group m-form__group row">
                <label class="col-form-label col-lg-3 col-sm-12"><@spring.message code="notification.content"/><span class="required"> * </span></label>
                <div class="col-lg-4 col-md-9 col-sm-12">
                    <textarea class="form-control" rows="3" name="content" required>${(m.content)!}</textarea>
                </div>
            </div>
        </div>
        <div class="m-portlet__foot m-portlet__foot--fit">
            <div class="m-form__actions m-form__actions--solid">
                <div class="row">
                    <div class="col-lg-9 ml-lg-auto">
                        <button data-toggle="m-tooltip" type="submit" class="btn m-btn m-btn--icon m-btn--wide m-btn--md btn-brand m--margin-right-10" title='<@spring.message code="button.submit"/>'> <span><i class="la la-check-circle"></i> <span><@spring.message code="button.submit"/></span></span></button>
                        <button data-toggle="m-tooltip" type="button" class="btn m-btn m-btn--icon m-btn--wide m-btn--md btn-info m--margin-right-10 btn-save-draft" title='<@spring.message code="op.save.draft" />'> <span><i class="la la-eraser"></i> <span><@spring.message code="op.save.draft" /></span></span></button>
                        <a data-toggle="m-tooltip" class="btn m-btn m-btn--icon m-btn--wide m-btn--md btn-secondary m--margin-right-10" href="<@BackURL/>" title='<@spring.message code="button.cancel"/>'> <span><i class="la la-ban"></i> <span><@spring.message code="button.cancel" /></span></span></a>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <!--end::Form-->
</div>
<page-plugin-style>
</page-plugin-style>
<page-plugin-script>
</page-plugin-script>
<page-style></page-style>
<page-script>
	<script>
		jQuery(document).ready(function() {
			var $form = $("#mainForm")
			
			var $name = $("#receiverId_msg");
			var $receiverId = $("#receiverId");
			if($name[0]){
				$.m.app.initAutocomplete({
		            input : $name,
		            source : "${ctx}/system/account/user/ajax/autocomplete",
		            select : function(event, ui) {
		                $receiverId.val(ui.item.value);
		                return false;
		            }
		        });
			}
	        
	        $(window).on('beforeunload',function() {
	            if($name.val() || $("#title").val() || editor.html()) {
	                return '<@spring.message code="confirm.leave.current.page"/>';
	            }
	        });

	        $form.submit(function() {
	            $(window).unbind("beforeunload");
	        });
	        
	        $(".btn-save-draft").click(function() {
	            //必须调用 尽管 form.submit绑定了释放，但是 form.validationEngine("detach"); 会off掉
	            $(window).unbind("beforeunload");
	            //$form.validationEngine("detach");
	            $form.attr("action", "${ctx}/personal/message/draft/save").submit();
	        });
		});
	</script>
</page-script>